<?php

class sgePDF extends TCPDF
{

	private $report_title = '';
	private $filename = '';

	public function _construct()
	{
		parent::_construct(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	}

	public function setup($report_title, $filename = 'reporte.pdf', $orientation = PDF_PAGE_ORIENTATION)
	{
		$this->report_title = $report_title;
		$this->filename = $filename;

		$this->SetCreator(PDF_CREATOR);
		$this->SetAuthor('Sistema gestión estadísticas');
		$this->SetTitle($this->report_title);
		$this->SetSubject($this->report_title);

		$this->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		$this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$this->SetHeaderMargin(15);
		$this->SetFooterMargin(PDF_MARGIN_FOOTER);

		$this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

		$this->SetFont('helvetica', '', 10);

		$this->AddPage($orientation);
	}

	public function createDocument()
	{
		$this->Output($this->filename, 'I');
	}

	public function writeReportTitle()
	{
		$this->setFont($this->getFontFamily(), 'B', 11);

		$this->Write(0, strtoupper($this->report_title), '', false, 'C');

		$this->setFont($this->getFontFamily(), '', 10);

		$this->Ln(10);
	}

	public function writeSubtitle($text)
	{
		$this->setFont($this->getFontFamily(), 'B', 11);

		$this->Write(0, $text, '', false, 'C');

		$this->setFont($this->getFontFamily(), '', 10);

		$this->Ln(10);
	}

	public function writeLabel($label, $new_line = false)
	{
		$this->setFont($this->getFontFamily(), 'B');
		$this->Cell(50, 0, $label, 0, 0, 'L', false, '', 1);
		$this->setFont($this->getFontFamily(), '');

		if ($new_line)
			$this->Ln(8);
	}
	
	public function writeText($text)
	{
		$this->Write(0, "$text\n", '', false, 'J', true);
		$this->Ln();
	}

	public function writeLine($label, $text)
	{
		$this->writeLabel($label);
		$this->writeText($text);
	}

	public function writeTable($header, $data, $w = array())
	{
		$count = count($header);
		$width = $this->getRemainingWidth() / $count;

		$this->setFont($this->getFontFamily(), 'B', 10);

		foreach ($header as $key => $value)
		{
			$this->Cell($width, 0, $value['label'], 1, 0, 'C', 0);
		}

		$this->Ln();
		$this->setFont($this->getFontFamily(), '', 10);

		foreach ($data as $index => $row)
		{
			foreach ($header as $key => $label)
			{
				$this->Cell($width, 0, $row[$key], 1, 0, 'C', 0, '', 1);
			}
			
			$this->Ln();
		}
	}

	public function writeFilters($filters)
	{
		$index = 0;
		$width = $this->getRemainingWidth() / 4; //2 columnas

		$this->writeSubtitle('Filtros de búsqueda');
		
		foreach ($filters as $filter)
		{
			$this->setFont($this->getFontFamily(), 'B');
			$this->Cell($width - 10, 0, $filter['label'], 0, 0, 'L', false, '', 1);
			$this->setFont($this->getFontFamily(), '');
			$this->Cell($width + 10, 0, $filter['value'] ? $filter['value'] : '-', 0, 0, 'L', false, '', 1);

			$index++;
			if ($index % 2 == 0)
				$this->Ln(5);
		}

		$this->Ln(10);
	}

	public function Header()
	{
		$this->SetFont($this->getFontFamily(), 'I', 8);
		$this->Cell(0, 15, 'Sistema de gestión de estadísticas', 0, false, 'R', 0, '', 0, false, 'M', 'M');
		$this->Ln(5);
		$this->Cell(0, 15, 'Fecha: ' . date('Y-m-d H:i:s'), 0, false, 'R', 0, '', 0, false, 'M', 'M');
	}

	public function Footer()
	{
		$this->SetY(-15);
		$this->SetFont($this->getFontFamily(), 'I', 8);
		$this->Cell(0, 10, 'Página ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
	}

}
